package com.esun.cheetah.view.personal;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.util.log.EDebug;
/**
 * 
 * @Package com.esun.cheetah.view.personal.PersonalAct
 * @ClassName: PersonalAct
 * @Description: TODO(personal activity group)
 * @author handy <br/>
 * @date create at 2012-7-31 下午3:01:13
 *
 */
public class PersonalAct extends ActivityGroup {
	/** Called when the activity is first created. */
	private LinearLayout bodyView;
	// 通过标记跳转不同的页面，显示不同的菜单项
	
	private String shareUserName;// 保存的用户名
	private String shareUserpass;// 保存的密码
	private SharedPreferences sharedPreferences;
	
	public static int flag = Language.FLAG_FIVE;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal);
		initMainView();
		readSharedPreferences();
		if (UserInfo.getUserInfo().getCk().length()<3) {
			Intent intent = getIntent();
			showView(flag, intent);
		}else if (null == shareUserName && null == shareUserpass) {
			
			Intent intent = getIntent();
			showView(flag, intent);
			EDebug.o("loginAct");
		
		}else {
			Intent intent = getIntent();
			showView(Language.FLAG_SIX, intent);
		}
//		Intent intent = getIntent();
//		showView(flag, intent);
		
//		if (null == shareUserName && null == shareUserpass) {
//			Intent intent = getIntent();
//			showView(flag, intent);
//			EDebug.o("loginAct");
//		} else {
//			Intent intent = getIntent();
//			showView(Language.FLAG_SIX, intent);
//			
//		}
	}
	
	
	@Override
	protected void onStart() {
		super.onStart();
		if (UserInfo.getUserInfo().getCk().length()<3) {
			Intent intent = getIntent();
			showView(flag, intent);
		}else if (null == shareUserName && null == shareUserpass) {
			
			Intent intent = getIntent();
			showView(flag, intent);
			EDebug.o("loginAct");
		
		}else {
			Intent intent = getIntent();
			showView(Language.FLAG_SIX, intent);
		}
	}


	@Override
	protected void onRestart() {
		super.onRestart();
		readSharedPreferences();
		if (UserInfo.getUserInfo().getCk().length()<3) {
			Intent intent = getIntent();
			showView(flag, intent);
		}else if (null == shareUserName && null == shareUserpass) {
			
			Intent intent = getIntent();
			showView(flag, intent);
			EDebug.o("loginAct");
		
		}else {
			Intent intent = getIntent();
			showView(Language.FLAG_SIX, intent);
		}
	}


	private void readSharedPreferences() {
		sharedPreferences = getSharedPreferences(Key.SHAREDPREFERENCES_NAME,
				Context.MODE_PRIVATE);
		shareUserName = sharedPreferences.getString(Key.KEY_USERNAME, null);
		shareUserpass = sharedPreferences.getString(Key.SAVE_PASSWORD, null);// getString()第二个参数为缺省值，如果preference中不存在该key，将返回缺省值
		EDebug.o("name  ------  " + shareUserName);
		EDebug.o("pass  ------" + shareUserpass);
	}

	public void initMainView() {
		bodyView = (LinearLayout) findViewById(R.id.personal);

	}

	public void showView(int flag, Intent intent) {

		switch (flag) {

		case Language.FLAG_FIVE:
			// 登录界面
			bodyView.removeAllViews();
			View login = getLocalActivityManager().startActivity(
					"login",
					new Intent(PersonalAct.this, LoginAct.class)
							.putExtras(intent)).getDecorView();
			EDebug.o("intent1-------" + intent);

			bodyView.addView(login, LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		case Language.FLAG_SIX:
			// 加载个人中心页面
			bodyView.removeAllViews();
			View loggedin = getLocalActivityManager().startActivity(
					"loggedin",
					new Intent(PersonalAct.this, LoggedinAct.class)
							.putExtras(intent)).getDecorView();
			EDebug.o("intent2-------" + intent.getStringExtra("username"));
			EDebug.o("intent2-------" + intent.getDoubleExtra("money", 0.00));
			EDebug.o("intent2-------" + intent.getStringExtra("ck"));

			bodyView.addView(loggedin, LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return getParent().onKeyDown(keyCode, event);
	}
}
